package cn.xshi.sys.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.xshi.common.base.BaseResult;
import cn.xshi.common.base.BaseService;
import cn.xshi.common.util.ExceptionUtil;
import cn.xshi.common.util.JsonUtil;
import cn.xshi.common.util.StringUtil;
import cn.xshi.common.util.file.FileUtil;
import cn.xshi.sys.dao.SysDataDictionaryDao;
import cn.xshi.sys.form.SysDataDictionaryForm;
import cn.xshi.sys.init.GlobalPersistentComponent;
import cn.xshi.sys.model.SysDataDictionary;
import cn.xshi.sys.service.SysDataDictionaryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @Desc 数据字典
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@Service
@Slf4j
public class SysDataDictionaryServiceImpl extends BaseService implements SysDataDictionaryService {

	@Resource
    SysDataDictionaryDao sysDataDictionaryDao;

	@Resource
    GlobalPersistentComponent globalPersistentComponent;

	/**
	* 分页
	* @param sysDataDictionaryForm
	* @return
	*/
	public List<SysDataDictionary> getSysDataDictionaryListByCondition(SysDataDictionaryForm sysDataDictionaryForm) {
		try {
			return sysDataDictionaryDao.getSysDataDictionaryListByCondition(sysDataDictionaryForm);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(), e.getCause());
		}
	}

	/**
	 * 加载ListAll不分页
	 * @param condition
	 * @return
	 */
	public List<SysDataDictionary> getSysDataDictionaryListAllByCondition(Map<String,Object> condition){
		try {
			return sysDataDictionaryDao.getSysDataDictionaryListAllByCondition(condition);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		} 
	}

	/**
	* 查询对象
	* @param id
	* @return
	*/
	public SysDataDictionary getSysDataDictionaryById(String id){
		try {
			return sysDataDictionaryDao.getSysDataDictionaryById(id);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
	}

	/**
	* 添加
	* @param sysDataDictionary
	* @return
	*/
	public int addSysDataDictionary(SysDataDictionary sysDataDictionary){
		int i = 0;
		try {
			i = sysDataDictionaryDao.addSysDataDictionary(sysDataDictionary);
			globalPersistentComponent.initSysDataDictionary();
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
		return i;
	}

	/**
	* 修改
	* @param sysDataDictionary
	* @return
	*/
	public int updateSysDataDictionary(SysDataDictionary sysDataDictionary){
		int i = 0;
		try {
			i = sysDataDictionaryDao.updateSysDataDictionary(sysDataDictionary);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
		return i;
	}

	/**
	* 删除
	* @param condition 
	* @return
	*/
	public int delSysDataDictionary(Map<String,Object> condition){
		int i = 0;
		try {
			i = sysDataDictionaryDao.delSysDataDictionary(condition);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
		return i;
	}

	/**
	 * 导出
	 * @param response
	 * @param id
	 */
	public void exportDictionary(HttpServletResponse response, String id){
		try{
			if(StringUtil.isEmpty(id)){
				log.warn("未能获取到字典id");
				throw new ExceptionUtil("未能获取到字典id");
			}
			SysDataDictionary sysDataDictionary = sysDataDictionaryDao.getSysDataDictionaryById(id);
			if(null == sysDataDictionary){
				log.warn("未能获取到字典信息");
				throw new ExceptionUtil("未能获取到字典信息");
			}
			Map<String,Object> condition = new HashMap<>();
			condition.put("parentId",id);
			List<SysDataDictionary> sysDataDictionaries = sysDataDictionaryDao.getSysDataDictionaryListAllByCondition(condition);
			sysDataDictionaries.add(sysDataDictionary);
			String json = JsonUtil.toFastJson(sysDataDictionaries);
			FileUtil.exportJsonFile(response,json,sysDataDictionary.getName()+"-"+sysDataDictionary.getName()+".json");
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
	}

	/**
	 * 导入
	 * @param multipartFile
	 * @return
	 */
	public BaseResult importDictionary(HttpServletRequest request, HttpServletResponse response, MultipartFile multipartFile){
		String jsonText = FileUtil.readJson(multipartFile);
		if(StringUtil.isEmpty(jsonText)){
			return BaseResult.fail("导入内容为空！");
		}
		List<SysDataDictionary> sysDataDictionaries = JsonUtil.toFastList(jsonText,SysDataDictionary.class);
		if(CollectionUtil.isEmpty(sysDataDictionaries)){
			return BaseResult.fail("未能解析到字典信息！");
		}
		for(SysDataDictionary sysDataDictionary : sysDataDictionaries){
			if(StringUtil.isEmpty(sysDataDictionary.getId())){
				return BaseResult.fail("未能解析到字典id！");
			}
			if(StringUtil.isEmpty(sysDataDictionary.getName())){
				return BaseResult.fail("未能解析到字典名称！");
			}
			SysDataDictionary dictionary = getSysDataDictionaryById(sysDataDictionary.getId());
			if(null == dictionary){//新增
				addSysDataDictionary(sysDataDictionary);
			}else{
				sysDataDictionary.setUpdateId(getCurrentAccountId());
				sysDataDictionary.setUpdateTime(getDate());
				updateSysDataDictionary(sysDataDictionary);
			}
		}
		return BaseResult.success();
	}
}
